package com.szqhyxj.youxiangju.biz.card.addCard;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;

import com.szqhyxj.youxiangju.biz.card.CardModel;
import com.szqhyxj.youxiangju.biz.core.ImageUploadResult;
import com.szqhyxj.youxiangju.biz.core.ImageUploader;
import com.szqhyxj.youxiangju.biz.realNameAuth.RealNameModel;
import com.szqhyxj.youxiangju.biz.realNameAuth.bankcard.BankList;
import com.szqhyxj.youxiangju.biz.realNameAuth.bankcard.CardInfo;
import com.szqhyxj.youxiangju.sys.AbBasePresenter;
import com.szqhyxj.youxiangju.sys.BaseResult;
import com.szqhyxj.youxiangju.sys.util.BitmapCompressFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * XiaoPanYu
 * on 2018/10/31
 */
public class BindCardPresenter extends AbBasePresenter implements BinCardContarct.Presenter{

    private final BinCardContarct.View view;
    private List<BankList> bankLists;
    private BindCardEntity bindCardEntity;
    private File faceFile;

    public BindCardPresenter(BinCardContarct.View  view){
        this.view = view;
        bindCardEntity = new BindCardEntity();
    }


    @Override
    public void onDestroy(){

    }

    public void init(){}

    @Override
    public void present(){

        String phone = bindCardEntity.getCardPhone();

        if(bindCardEntity.getFile()==null){
            view.showToast("无效照片");
            return;
        }

        if(TextUtils.isEmpty(phone)||phone.length()<11){
            view.showToast("请输入正确手机号");
            return;
        }

        String issuingBank = bindCardEntity.getBtName();
        if(TextUtils.isEmpty(issuingBank)||issuingBank.length()==0){

            view.showToast("请选择发卡行");
            return;
        }

        String cardNo = bindCardEntity.getBankCardNo();
        if(TextUtils.isEmpty(cardNo)||cardNo.length()<11){

            view.showToast("请输入卡号");
            return;
        }

        String name = bindCardEntity.getUsername();
        if(TextUtils.isEmpty(name)||name.length()==0){

            view.showToast("请输入用户名");
            return;
        }

        String periodOfValidity = bindCardEntity.getExpiryDate();
        if(TextUtils.isEmpty(periodOfValidity)||periodOfValidity.length()!=4){

            view.showToast("请输入有效期");
            return;
        }

        String cvn = bindCardEntity.getCvn();
        if(TextUtils.isEmpty(cvn)||cvn.length()<3){

            view.showToast("请输入CVN2");
            return;
        }


        String accountantBillDate = bindCardEntity.getBillingDate();
        if(TextUtils.isEmpty(accountantBillDate)){
            view.showToast("请设置账单日");
            return;
        }

        String tvDueDate = bindCardEntity.getDueDate();
        if(TextUtils.isEmpty(tvDueDate)){
            view.showToast("请设置还款日");
            return;
        }

        String fixedQuota = bindCardEntity.getFixedAmount();
        if(TextUtils.isEmpty(fixedQuota)){
            view.showToast("请输入固定额度");
            return;
        }

        String code = bindCardEntity.getSendValidCode();
        if(TextUtils.isEmpty(code)||code.length()<4){
            view.showToast("请输入有效验证码");
            return;
        }

        upPhotograph(bindCardEntity.getFile());
    }


    public void upInformation (){
        addDisposables(BindCardModel.addBankCard(bindCardEntity)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<BaseResult>(){
                    @Override
                    public void accept(BaseResult baseResult) throws Exception{
                        view.dismissLoading();
                        if(baseResult.businessLogicSuccess()){
                            view.navSuccess(baseResult.getMessage());
                        }else {
                            view.navErr(baseResult.getMessage());
                        }
                    }
                }, new Consumer<Throwable>(){
                    @Override
                    public void accept(Throwable throwable) throws Exception{
                        view.navErr(throwable.getMessage());
                        view.dismissLoading();
                    }
                }));
    }


    @Override
    public void setIssuingBank(int i){
        BankList bank = bankLists.get(i);
        setBtCode(bank.getBtCode());
        setBtName(bank.getBtName());
    }

    @Override
    public void getIssuingBank(){
        view.showLoading();
        addDisposables(CardModel.getBankList()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<BaseResult<List<BankList>>>(){
                    @Override
                    public void accept(BaseResult<List<BankList>> listBaseResult) throws Exception{
                        view.dismissLoading();
                        if (listBaseResult.businessLogicSuccess()){
                            bankLists = listBaseResult.getData();
                            String[] banks = new String[bankLists.size()];
                            for(int i = 0; i< bankLists.size(); i++){
                                banks[i] = bankLists.get(i).getBtName();
                            }
                            view.showIssuingBank(banks);
                        }else {
                            view.showToast(listBaseResult.getMessage());
                        }

                    }
                }, new Consumer<Throwable>(){
                    @Override
                    public void accept(Throwable throwable) throws Exception{
                        view.showToast(throwable.getMessage());
                        view.dismissLoading();
                    }
                })
        );
    }

    @Override
    public void getPhoneCode(){
        view.showPhoneCode();
    }

    @Override
    public void bankCreditCard(){
        view.showLoading();
        addDisposables(BindCardModel.addBankCardSend(bindCardEntity.getCardPhone())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<BaseResult>(){
                    @Override
                    public void accept(BaseResult baseResult) throws Exception{
                        view.dismissLoading();
                        if(baseResult.businessLogicSuccess()){
                            view.showToast("获取验证码成功");
                        }else{
                            view.showToast(baseResult.getMessage());
                        }

                    }
                }, new Consumer<Throwable>(){
                    @Override
                    public void accept(Throwable throwable) throws Exception{
                        view.showToast(throwable.getMessage());
                        view.dismissLoading();
                    }
                })
        );
    }

    @Override
    public void ocrPhotograph(final File file){
        view.showLoading("正在识别..");
        addDisposables((
        BindCardModel.bankCreditCard(file)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<BaseResult<CardInfo>>(){
                    @Override
                    public void accept(BaseResult<CardInfo> baseResult) throws Exception{
                        if(baseResult.businessLogicSuccess()){
                            upPhotograph(file);
                            view.showCardNo(baseResult.getData().getCardNo());
                            bindCardEntity.setBankCardNo(baseResult.getData().getCardNo());
                        }else {

                            view.showToast(baseResult.getMessage());
                        }
                    }
                }, new Consumer<Throwable>(){
                    @Override
                    public void accept(Throwable throwable) throws Exception{
                        view.dismissLoading();
                        view.showToast(throwable.getMessage());
                    }
                })
        ));
    }

    @Override
    public void upPhotograph(final File file){
        view.showLoading("正在上传...");
        File tfile = BitmapCompressFile.compressFile(file);
        addDisposables(
                ImageUploader.uploadCredit(tfile,bindCardEntity.getBankCardNo())
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<BaseResult<ImageUploadResult>>(){
                    @Override
                    public void accept(BaseResult<ImageUploadResult> imageUploadResultBaseResult) throws Exception{
                        view.dismissLoading();
                        if(imageUploadResultBaseResult.businessLogicSuccess()){
                            setBankCardPic(imageUploadResultBaseResult.getData().getFileUrl());
                            upInformation();
                        }else {
                            view.showToast(imageUploadResultBaseResult.getMessage());
                        }
                        tfile.delete();
                    }
                }, new Consumer<Throwable>(){
                    @Override
                    public void accept(Throwable throwable) throws Exception{

                        view.dismissLoading();
                        view.showToast(throwable.getMessage());
                        tfile.delete();
                    }
                })
        );
    }

    @Override
    public void idcardFromGallery(Context mContext,Intent intent){
        Uri selectedImage = intent.getData();
        File file = new File(selectedImage.getAuthority());
        view.previewFace(file);

    }

    @Override
    public void takePhotoFor(Context context) throws IOException{
        faceFile = null;

        faceFile = RealNameModel.createImageFile(context);

        view.navToTakePhoto(faceFile);
    }

    @Override
    public void selectFromGalleryFor(){
        view.navToGalleryFor();
    }

    @Override
    public void setDueDate(String dueDate){
        view.showDueDate(dueDate);
        bindCardEntity.setDueDate(dueDate);
    }

    @Override
    public void setAccountantBillDate(String accountantBillDate){
        view.showAccountantBillDate(accountantBillDate);
        bindCardEntity.setBillingDate(accountantBillDate);
    }

    @Override
    public void setBtCode(String btCode){
        bindCardEntity.setBtCode(btCode);
    }

    @Override
    public void setBtName(String btName){
        bindCardEntity.setBtName(btName);
    }

    @Override
    public void setUsername(String username){
        bindCardEntity.setUsername(username);
    }

    @Override
    public void setBankCardNo(String bankCardNo){
        bindCardEntity.setBankCardNo(bankCardNo);
    }

    @Override
    public void setCardPhone(String cardPhone){
        bindCardEntity.setCardPhone(cardPhone);
    }

    @Override
    public void setBankCardPic(String bankCardPic){
        bindCardEntity.setBankCardPic(bankCardPic);
    }

    @Override
    public void setCvn(String cvn){
        bindCardEntity.setCvn(cvn);
    }

    @Override
    public void setExpiryDate(String expiryDate){
        bindCardEntity.setExpiryDate(expiryDate);
    }

    @Override
    public void setBillingDate(String billingDate){
        bindCardEntity.setBillingDate(billingDate);
    }

    @Override
    public void setFixedAmount(String fixedAmount){
        bindCardEntity.setFixedAmount(fixedAmount);
    }

    @Override
    public void setSendValidCode(String sendValidCode){
        bindCardEntity.setSendValidCode(sendValidCode);

    }

    @Override
    public void setTempFile(File file){
        bindCardEntity.setFile(file);
    }

    @Override
    public void addDisposables(Disposable disposable){

        super.addDisposables(disposable);
    }
}
